var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

document.getElementById( 'Ambush_Your_Race'      ).value = prefService.getIntPref( 'extensions.Ultima.YourThroneRace'           );
document.getElementById( 'Ambush_Your_Soldiers'  ).value = prefService.getIntPref( 'extensions.Ultima.YourThroneSoldiers'       );
document.getElementById( 'Ambush_Your_OffSpecs'  ).value = prefService.getIntPref( 'extensions.Ultima.YourThroneOffensiveUnits' );
document.getElementById( 'Ambush_Your_Elites'    ).value = prefService.getIntPref( 'extensions.Ultima.YourThroneEliteUnits'     );
document.getElementById( 'Ambush_Your_Horses'    ).value = prefService.getIntPref( 'extensions.Ultima.YourThroneWarHorses'      );
document.getElementById( 'Ambush_Your_Merc_Pris' ).value = prefService.getIntPref( 'extensions.Ultima.YourThronePrisoners'      );

function Task_Ambush() {

var Ambush_Your_Race      = 0 ;
var Ambush_Your_Soldiers  = 0 ;
var Ambush_Your_OffSpecs  = 0 ;
var Ambush_Your_Elites    = 0 ;
var Ambush_Your_Horses    = 0 ;
var Ambush_Your_Merc_Pris = 0 ;
var Ambush_Your_Offense   = 0 ;

var Ambush_Enemy_Race     = 0 ;
var Ambush_Enemy_Soldiers = 0 ;
var Ambush_Enemy_OffSpecs = 0 ;
var Ambush_Enemy_Elites   = 0 ;
var Ambush_Enemy_Defense  = 0 ;

var Ambush_Your_Race      =         document.getElementById( 'Ambush_Your_Race'      ).value ;
var Ambush_Your_Soldiers  = Number( document.getElementById( 'Ambush_Your_Soldiers'  ).value );
var Ambush_Your_OffSpecs  = Number( document.getElementById( 'Ambush_Your_OffSpecs'  ).value );
var Ambush_Your_Elites    = Number( document.getElementById( 'Ambush_Your_Elites'    ).value );
var Ambush_Your_Horses    = Number( document.getElementById( 'Ambush_Your_Horses'    ).value );
var Ambush_Your_Merc_Pris = Number( document.getElementById( 'Ambush_Your_Merc_Pris' ).value );

var Ambush_Enemy_Race     =         document.getElementById( 'Ambush_Enemy_Race'     ).value ;
var Ambush_Enemy_Soldiers = Number( document.getElementById( 'Ambush_Enemy_Soldiers' ).value );
var Ambush_Enemy_OffSpecs = Number( document.getElementById( 'Ambush_Enemy_OffSpecs' ).value );
var Ambush_Enemy_Elites   = Number( document.getElementById( 'Ambush_Enemy_Elites'   ).value );

Ambush_Your_Offense  = Ambush_Your_Soldiers  * Offensive_Soldiers_Strength[Ambush_Your_Race]  + Offensive_Specialist_Strength[Ambush_Your_Race]  * Ambush_Your_OffSpecs  + Offensive_Elite_Unit_Strength[Ambush_Your_Race]  * Ambush_Your_Elites + Ambush_Your_Horses + Ambush_Your_Merc_Pris * 3 ;
Ambush_Enemy_Defense = Ambush_Enemy_Soldiers * Defensive_Soldiers_Strength[Ambush_Enemy_Race] + Defensive_Specialist_Strength[Ambush_Enemy_Race] * Ambush_Enemy_OffSpecs + Defensive_Elite_Unit_Strength[Ambush_Enemy_Race] * Ambush_Enemy_Elites ;

Ambush_Enemy_Defense = Math.round( Ambush_Enemy_Defense * 0.8 );

document.getElementById( 'Ambush_Your_Offense'  ).innerHTML = Ambush_Your_Offense  ;
document.getElementById( 'Ambush_Enemy_Defense' ).innerHTML = Ambush_Enemy_Defense ;

if (( Ambush_Your_Offense == 0 )||( Ambush_Enemy_Defense == 0 )) document.getElementById( 'Ambush_Results_IMG' ).src = "chrome://Ultima/content/Ambush_Mark.png";

if (( Ambush_Your_Offense == 0 )&&( Ambush_Enemy_Defense == 0 )) document.getElementById( 'Ambush_Results_MSG' ).innerHTML = "Complete information required on both sides";
if (( Ambush_Your_Offense == 0 )&&( Ambush_Enemy_Defense  > 0 )) document.getElementById( 'Ambush_Results_MSG' ).innerHTML = "Attacker's info is missing";
if (( Ambush_Your_Offense  > 0 )&&( Ambush_Enemy_Defense == 0 )) document.getElementById( 'Ambush_Results_MSG' ).innerHTML = "Defender's info is missing";
if (( Ambush_Your_Offense  > 0 )&&( Ambush_Enemy_Defense  > 0 )) {

var temp = Math.round( ( Ambush_Your_Offense / Ambush_Enemy_Defense - 1 ) * 10000 ) / 100 ;

document.getElementById( 'Ambush_Results_MSG' ).innerHTML = "You have "+temp+"% than enemy's Defensive strength" ;

if ( Ambush_Your_Offense > Ambush_Enemy_Defense ) document.getElementById( 'Ambush_Results_IMG' ).src = "chrome://Ultima/content/Ambush_Success.png";
else document.getElementById( 'Ambush_Results_IMG' ).src = "chrome://Ultima/content/Ambush_Fail.png";

}

}

function Task_Manager() {
Task_Ambush();
setTimeout("Task_Manager()",1);
}

Task_Manager();